/**
 * Created by Eoly on 2017/1/1.
 * 子弹类
 */
function Bullet() {
    var _this = this;

    //成员变量

    //需要外部赋值
    _this.playerBelongs
        = null;

    //需调用set函数初始化
    _this.centrePoint
        = null;
    _this.bulletCanvas  //子弹画布
        = null;


    //默认赋值
    _this.modelUsed
        = new BulletModel();
    _this.maxHP
        = 10;
    _this.HP
        = 10;
    _this.runningTrack  //运行轨迹
        = new BulletRunningTrack();
    _this.bombRadius    //爆炸半径
        = this.modelUsed.radius * 2;
    _this.isShooted
        = false;
    _this.deathEffects
        = new ArcEffects();
    _this.isDeath   //是否死亡
        = false;
    _this.isDestroied   //是否完全摧毁
        = false;
    this.needToDraw     //是否需要在画布上绘制
        = true;
    _this.bombDamage
        = 10;
    _this.collisionDamage   //碰撞伤害
        = 300;
    _this.init = function () {
        //初始化设置
        _this.deathEffects.fillColor.setRGBA(255, 100, 0, 0.8);
        _this.deathEffects.strokeColor.setRGBA(255, 100, 0, 0.8);
        _this.deathEffects.max_r = _this.bombRadius;
        // console.log(this+_this.bombRadius);
        _this.deathEffects.risingIndex = 1.2;
    };

    this.init();
}

Bullet.prototype = {

    constructor: Bullet,

    setCentrePoint: function (point) {
        this.centrePoint = point;
        this.runningTrack.centrePoint = point;
        this.deathEffects.point = this.centrePoint;
    },

    setBulletCanvas: function (canvas) {
        this.bulletCanvas = canvas;
        canvas.objs.push(this);
        canvas.allObj.objs.push(this);
    },

    beInjured: function (damage) {
        this.HP -= damage;
        if (this.HP <= 0) {
            this.isDeath = true;
            if (this.playerBelongs.id == 1) {
                player.mark += 50;
                player.writeInfo();
            }
        }
    },

    isInnerWindows: function () {
        var width = gameBox.offsetWidth;
        var height = gameBox.offsetHeight;

        var destroyPoint = new Point(width * 0.1, height * 0.1);

        var isInnerWindows = true;

        isInnerWindows = isInnerWindows && this.centrePoint.x < width + destroyPoint.x;
        isInnerWindows = isInnerWindows && this.centrePoint.x > 0 - destroyPoint.x;
        isInnerWindows = isInnerWindows && this.centrePoint.y < height + destroyPoint.y;
        // return true;
        return isInnerWindows && this.centrePoint.y > 0 - destroyPoint.y;
    },

    trackChanges: function () {
        //改变弹道事件
    },

    runWithTrack: function () {
        if (!this.isDeath) {
            this.trackChanges();
            this.setCentrePoint(this.runningTrack.trackStep());
            if (this.modelUsed.isFaceDeg)
                this.modelUsed.faceDeg = this.runningTrack.direction;
            if (!this.isInnerWindows()) {
                this.needToDraw = false;
                this.isDestroied = true;
            }
        }

        if (this.isDestroied)this.isDeath = true;
        if (this.isDestroied) {
            this.needToDraw = false;
            delete this;
        }

    },

    draw: function () {
        var _this = this;
        var ctx = this.bulletCanvas.ctx;
        this.runWithTrack();

        if (!this.isDeath) {
            ctx.beginPath();

            //绘制image
            this.modelUsed.preImg(function () {

                var winHeight = window.innerHeight;
                var winWidth = window.innerWidth;
                var model = _this.modelUsed;
                var img = model.img;
                var realWidth = winWidth * model.size;
                var realHeight = winHeight * model.size * img.height / img.width;
                var correctPoint = new Point(_this.centrePoint.x - realWidth * 0.5, _this.centrePoint.y - realHeight * 0.5);

                ctx.save();
                ctx.translate(_this.centrePoint.x, _this.centrePoint.y);
                ctx.rotate(_this.modelUsed.faceDeg * Math.PI / 180);//调整面向角度
                ctx.translate(-_this.centrePoint.x, -_this.centrePoint.y);
                ctx.drawImage(img, correctPoint.x, correctPoint.y, realWidth, realHeight);
                ctx.restore();

            });
        } else {
            //死亡特效
            this.deathEffects.draw(ctx);
            if (this.deathEffects.isEnd) {
                this.isDestroied = true;
            }
        }
    }
};

function RandomBullet() {
    Bullet.call(this);
    this.collisionDamage
        = 60;
}

Extend(Bullet, RandomBullet);

function RandomSBullet() {
    Bullet.call(this);
    this.collisionDamage
        = 60;
}

Extend(Bullet, RandomSBullet);
RandomSBullet.prototype.trackChanges = function () {
    this.runningTrack.direction = 180 * (Math.random() - 0.5);
};


function RayBullet() {
    Bullet.call(this);
    this.collisionDamage
        = 500;
    this.HP = 1000;
    this.runningTrack.acceleration = 0;
    this.runningTrack.speed = 10;
    this.modelUsed = new RayBulletModel();
}

Extend(Bullet, RayBullet);

function EnemyFollowBullet() {
    Bullet.call(this);
    this.collisionDamage = 100;
    this.modelUsed = new EnemyBulletModel();
    this.runningTrack = new EnemyFallowTrack();
}

Extend(Bullet, EnemyFollowBullet);

function EnemyNormalBullet() {
    Bullet.call(this);
    this.collisionDamage = 200;
    this.modelUsed = new EnemyBulletModel();
    this.runningTrack = new EnemyNormalTrack();
}

Extend(Bullet, EnemyNormalBullet);

function RandomFollowBullet() {
    RandomBullet.call(this);
    this.runningTrack = new RandomFollowTrack();
    this.collisionDamage = 200;
    this.HP = 1500;
    this.modelUsed.size = 0.020;
}

Extend(RandomBullet, RandomFollowBullet);

function DefenseBullet() {
    var _this = this;
    Bullet.call(this);
    this.runningTrack = new DefenseTrack();
    this.collisionDamage = 500;
    this.HP = 300;
    this.modelUsed = new DefenseModel();
    this.deathEffects = new ArcEffects();
    this.init = function () {

        _this.deathEffects.fillColor.setRGBA(150, 150, 255, 0.8);
        _this.deathEffects.strokeColor.setRGBA(150, 150, 255, 1);
        _this.deathEffects.max_r = _this.bombRadius;
        _this.deathEffects.risingIndex = 1.2;
    };

    this.init();
}

Extend(Bullet, DefenseBullet);